#! /usr/bin/python
#coding=utf-8
import hashlib,re,sys
from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker
from bs4 import BeautifulSoup
from  datetime  import  *
import gzip,urllib2,random,time
import urllib
import StringIO
import codecs #处理"°C" 这样的符号用
look  =  codecs.lookup( " utf-8 " )
# i=look.decode(i)

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)
print sys.getdefaultencoding();


class Product(object):
    __name__='product'
    id=Column(Integer, primary_key=True)
    name=Column(CHAR(255))
    url_code=Column(Unicode(255))
    url=Column(Unicode(255))
    image=Column(Unicode(255))
    price=Column(Unicode(255))
    cat_code=Column(Unicode(255))
    page=Column(Integer)
    site=Column(Unicode(255))

class ProductExt(object):
    __name__='product_ext'
    id=Column(Integer, primary_key=True)
    product_id=Column(Integer)
    image=Column(Text)
    summary=Column(Text)
    description_m=Column(Text)

class Cat:
    def __init__(self):
        self.engine =create_engine('mysql://root:123456@localhost/focal?charset=utf8',echo=True)
        #self.engine =create_engine('mysql://focal:focalweiming@localhost/focal?charset=utf8',echo=True)
        metadata = MetaData()

        product_table=Table('product',metadata,
            Column('id',Integer, primary_key=True),
            Column('name',CHAR(255)),
            Column('url_code',Unicode(255)),
            Column('url',Unicode(255)),
            Column('cat_code',Unicode(255)),
            Column('image',Unicode(255)),
            Column('price',Unicode(255)),
            Column('page',Integer),
            Column('site',Unicode(255)),
        )
        productext_table=Table('product_ext',metadata,
            Column('id',Integer, primary_key=True),
            Column('product_id',Integer),
            Column('image',Text),
            Column('summary',Text),
            Column('description_m',Text),
        )
        metadata.create_all(self.engine)
        mapper(Product,product_table)
        mapper(ProductExt,productext_table)
        Session = sessionmaker()
        Session.configure(bind=self.engine)
        self.db = Session()
